//
//  UITextField+Tool.swift
//  ltsp
//
//  Created by jyvyi_ios on 2022/5/23.
//

import Foundation
//MARK: 实现多个输入框内容判断，获取所有输入框是否完成内容填写，返回所有
class UITextFieldCompletableFlag : NSObject{
    
    var contentChangedBlock : ReturnValueBlock<Bool>?
    
    fileprivate var registerTextFields : [UITextField] = []
    
    func registerTextField(_ textField:UITextField){
        
        guard !self.registerTextFields.contains(textField) else {
            return
        }
        textField.addTarget(self, action: #selector(textFieldChanged(_:)), for: .editingChanged)
        self.registerTextFields.append(textField)
    }
    
    fileprivate func isAllInputCompleted() -> Bool{
        
        var isAllInput = true
        for textField in self.registerTextFields {
            if let text = textField.text , !text.isEmpty{
                continue
            }
            else{
                isAllInput = false
                break
            }
        }
        return isAllInput
    }
    
    @objc fileprivate func textFieldChanged(_ textField:UITextField){

        if let contentChangedBlock = contentChangedBlock {
            contentChangedBlock(self.isAllInputCompleted())
        }
    }
}

